import * as Knex from 'knex'

export async function up(knex: Knex): Promise<void> {
  // 用户表
  await knex.schema.createTable('users', (table) => {
    table.uuid('id').primary()
    table.string('username', 50).notNullable().unique()
    table.string('nickname', 50).notNullable()
    table.string('password_hash').notNullable()
    table.string('avatar_url')
    table.integer('balance').defaultTo(0)
    table.boolean('is_admin').defaultTo(false)
    table.boolean('is_vip').defaultTo(false)
    table.timestamp('created_at').defaultTo(knex.fn.now())
    table.timestamp('updated_at').defaultTo(knex.fn.now())
  })

  // 直播房间表
  await knex.schema.createTable('live_rooms', (table) => {
    table.uuid('id').primary()
    table.uuid('anchor_id').references('id').inTable('users')
    table.string('title', 100).notNullable()
    table.string('cover_url')
    table.string('stream_key').unique()
    table.string('rtmp_url')
    table.string('hls_url')
    table.enum('status', ['preparing', 'living', 'offline', 'banned']).defaultTo('preparing')
    table.integer('online_count').defaultTo(0)
    table.timestamp('created_at').defaultTo(knex.fn.now())
    table.timestamp('updated_at').defaultTo(knex.fn.now())
  })

  // 聊天消息表
  await knex.schema.createTable('chat_messages', (table) => {
    table.uuid('id').primary()
    table.uuid('room_id').references('id').inTable('live_rooms')
    table.uuid('user_id').references('id').inTable('users')
    table.text('content').notNullable()
    table.boolean('is_blocked').defaultTo(false)
    table.timestamp('created_at').defaultTo(knex.fn.now())
  })

  // 礼物记录表
  await knex.schema.createTable('gift_records', (table) => {
    table.uuid('id').primary()
    table.uuid('room_id').references('id').inTable('live_rooms')
    table.uuid('sender_id').references('id').inTable('users')
    table.uuid('receiver_id').references('id').inTable('users')
    table.string('gift_type', 50).notNullable()
    table.integer('gift_count').notNullable()
    table.integer('coin_amount').notNullable()
    table.timestamp('created_at').defaultTo(knex.fn.now())
  })

  // 支付订单表
  await knex.schema.createTable('payment_orders', (table) => {
    table.string('id').primary() // 订单号
    table.uuid('user_id').references('id').inTable('users')
    table.decimal('amount', 10, 2).notNullable()
    table.integer('coins').notNullable()
    table.enum('method', ['alipay', 'wechat']).notNullable()
    table.enum('status', ['pending', 'success', 'failed']).defaultTo('pending')
    table.timestamp('created_at').defaultTo(knex.fn.now())
    table.timestamp('paid_at')
  })

  // 用户关注表
  await knex.schema.createTable('user_follows', (table) => {
    table.uuid('follower_id').references('id').inTable('users')
    table.uuid('following_id').references('id').inTable('users')
    table.timestamp('created_at').defaultTo(knex.fn.now())
    table.primary(['follower_id', 'following_id'])
  })
}

export async function down(knex: Knex): Promise<void> {
  await knex.schema.dropTable('user_follows')
  await knex.schema.dropTable('payment_orders')
  await knex.schema.dropTable('gift_records')
  await knex.schema.dropTable('chat_messages')
  await knex.schema.dropTable('live_rooms')
  await knex.schema.dropTable('users')
}
